import { FormSchema } from '/@/components/Table';
import { getAsynKeyUser, getUnitTree, selectPersonTreeList } from './service';
import { initDictOptions } from '/@/utils/dict/index';

const statusOptionsList = ref([]);
initDictOptions('hub_org_type').then((res) => {
  statusOptionsList.value = res || [];
});

export const addForm: FormSchema[] = [
  {
    field: 'id',
    label: 'id',
    component: 'Input',
    show: false,
  },
  {
    field: 'orgName',
    label: '组织部门',
    component: 'Input',
    dynamicDisabled: () => true,
    colProps: { span: 8 },
  },
  {
    field: 'orgCode',
    label: '部门编码',
    component: 'Input',
    required: true,
    dynamicDisabled: () => true,
    colProps: { span: 8 },
  },
  {
    field: 'parentOrgName',
    label: '上级部门',
    component: 'Input',
    dynamicDisabled: () => true,
    colProps: { span: 8 },
  },
  {
    field: 'orgType',
    label: '组织类型',
    component: 'Input',
    dynamicDisabled: () => true,
    colProps: { span: 8 },
  },
  {
    field: 'pluraNameList',
    label: '绩效员',
    component: 'ApiTreeSelect',
    componentProps: ({ formModel }) => {
      return {
        api: getAsynKeyUser,
        parentCode: {
          key: 'id',
          parentKey: 'parentId',
        },
        fieldNames: {
          children: 'children',
          label: 'name',
          value: 'name',
        },
        // @ts-ignore
        onSelect: (text, node) => {
          formModel.pluraList = node.code;
          formModel.pluraNameList = node.name;
        },
        getPopupContainer: () => document.body,
      };
    },
    colProps: { span: 8 },
  },
  {
    field: 'psnName',
    label: '部门负责人',
    component: 'ApiTreeSelect',
    componentProps: ({ formModel }) => {
      return {
        api: getAsynKeyUser,
        parentCode: {
          key: 'id',
          parentKey: 'parentId',
        },
        fieldNames: {
          children: 'children',
          label: 'name',
          value: 'code',
        },
        // @ts-ignore
        onSelect: (text, node) => {
          console.log('node', node);
          formModel.psnName = node.name;
          formModel.psnNum = node.code;
        },
        getPopupContainer: () => document.body,
      };
    },
    colProps: { span: 8 },
  },
  {
    field: 'epmPerson1Name',
    label: '分管领导',
    component: 'ApiTreeSelect',
    componentProps: ({ formModel }) => {
      return {
        api: getAsynKeyUser,
        parentCode: {
          key: 'id',
          parentKey: 'parentId',
        },
        fieldNames: {
          children: 'children',
          label: 'name',
          value: 'name',
        },
        // @ts-ignore
        onSelect: (text, node) => {
          formModel.epmPerson1 = node.code;
        },
        getPopupContainer: () => document.body,
      };
    },
    colProps: { span: 8 },
  },
  {
    field: 'epmPerson2Name',
    label: '主要领导',
    component: 'ApiTreeSelect',
    componentProps: ({ formModel }) => {
      return {
        api: getAsynKeyUser,
        parentCode: {
          key: 'id',
          parentKey: 'parentId',
        },
        fieldNames: {
          children: 'children',
          label: 'name',
          value: 'name',
        },
        // @ts-ignore
        onSelect: (text, node) => {
          formModel.epmPerson2 = node.code;
        },
        getPopupContainer: () => document.body,
      };
    },
    colProps: { span: 8 },
  },
  {
    field: 'epmPerson3Name',
    label: '其它分管领导',
    component: 'ApiTreeSelect',
    componentProps: ({ formModel }) => {
      return {
        api: getAsynKeyUser,
        parentCode: {
          key: 'id',
          parentKey: 'parentId',
        },
        fieldNames: {
          children: 'children',
          label: 'name',
          value: 'name',
        },
        // @ts-ignore
        onSelect: (text, node) => {
          formModel.epmPerson3 = node.code;
        },
        getPopupContainer: () => document.body,
      };
    },
    colProps: { span: 8 },
  },
  {
    field: 'assess',
    label: '是否考核',
    component: 'ZSwitch',
    componentProps: {
      options: ['true', 'false'],
    },
    colProps: { span: 8 },
  },
  {
    field: 'isIntensification',
    label: '是否集约化',
    component: 'ZSwitch',
    componentProps: {
      options: ['true', 'false'],
    },
    colProps: { span: 8 },
  },
  {
    field: 'displayOrder',
    label: '排序',
    component: 'InputNumber',
    colProps: { span: 8 },
  },
];

export const editForm: FormSchema[] = [
  {
    field: 'id',
    label: 'id',
    component: 'Input',
    show: false,
  },
  {
    field: 'orgCode',
    label: '组织部门',
    component: 'Input',
    show: false,
  },
  {
    field: 'orgName',
    label: '组织部门',
    component: 'Input',
    dynamicDisabled: () => true,
  },
  {
    field: 'orgCode',
    label: '部门编码',
    component: 'Input',
    required: true,
    dynamicDisabled: () => true,
  },
  {
    field: 'parentOrgCode',
    label: '上级部门编码',
    component: 'Input',
    show: false,
  },
  {
    field: 'parentOrgName',
    label: '上级部门',
    component: 'Input',
    dynamicDisabled: () => true,
  },
  {
    field: 'orgType',
    label: '组织类型',
    component: 'ZDictSelectTag',
    componentProps: {
      dictCode: 'hub_org_type',
      placeholder: '请选择...',
    },
  },
  {
    label: '部门负责人',
    field: 'psnNum',
    required: false,
    component: 'ZSelectCustom',
    componentProps: ({ formModel }) => {
      console.log('formModel', formModel);
      return {
        sync: true,
        queryApi: selectPersonTreeList,
        titleKey: 'title',
        rowKey: 'psnNum',
        multiple: false, //多选
        modalTitle: '人员选择',
        maxTagCount: 3,
        // options: [{ label: formModel.managerName ? formModel.managerName : '', value: formModel.manager ? formModel.manager[0] : '' }],
        params: {
          type: '1',
        },
      };
    },
  },
  {
    field: 'psnName',
    label: '部门负责人名称',
    component: 'Input',
    show: false,
  },
  {
    field: 'psnCode',
    label: '部门负责人编码',
    component: 'Input',
    show: false,
  },
  // {
  //   field: 'psnName',
  //   label: '部门负责人',
  //   component: 'ApiTreeSelect',
  //   componentProps: ({ formModel }) => {
  //     return {
  //       api: getAsynKeyUser,
  //       parentCode: {
  //         key: 'id',
  //         parentKey: 'parentId',
  //       },
  //       fieldNames: {
  //         children: 'children',
  //         label: 'name',
  //         value: 'code',
  //       },
  //       // multiple: true,
  //       // @ts-ignore
  //       onChange: (value, label, extra) => {
  //         formModel.psnNum = [];
  //         formModel.psnDesc = [];
  //         extra.allCheckedNodes.forEach((item) => {
  //           formModel.psnNum.push(item.node.props.code);
  //           formModel.psnDesc.push(item.node.props.name);
  //         });
  //       },
  //       getPopupContainer: () => document.body,
  //     };
  //   },
  // },
  // {
  //   field: 'psnNum',
  //   label: '部门负责人名称',
  //   component: 'Input',
  //   show: false,
  // },
  // {
  //   field: 'psnDesc',
  //   label: '部门负责人名称',
  //   component: 'Input',
  //   show: false,
  // },
  {
    label: '绩效员',
    field: 'pluraList',
    required: false,
    component: 'ZSelectCustom',
    componentProps: ({ formModel }) => {
      console.log('formModel', formModel);
      return {
        sync: true,
        queryApi: selectPersonTreeList,
        titleKey: 'title',
        rowKey: 'psnNum',
        multiple: false, //多选
        modalTitle: '人员选择',
        maxTagCount: 3,
        // options: [{ label: formModel.managerName ? formModel.managerName : '', value: formModel.manager ? formModel.manager[0] : '' }],
        params: {
          type: '1',
        },
      };
    },
  },
  {
    field: 'pluraNameList',
    label: '绩效员名称',
    component: 'Input',
    show: false,
  },
  {
    field: 'pluraListCode',
    label: '绩效员编码',
    component: 'Input',
    show: false,
  },
  {
    field: 'epmPerson1Name',
    label: '分管领导名称',
    component: 'Input',
    show: false,
  },
  {
    field: 'epmPerson1Code',
    label: '分管领导编码',
    component: 'Input',
    show: false,
  },
  {
    label: '分管领导',
    field: 'epmPerson1',
    required: false,
    component: 'ZSelectCustom',
    componentProps: ({ formModel }) => {
      console.log('formModel', formModel);
      return {
        sync: true,
        queryApi: selectPersonTreeList,
        titleKey: 'title',
        rowKey: 'psnNum',
        multiple: true, //多选
        modalTitle: '人员选择',
        showSearch: true,
        maxTagCount: 3,
        // options: [{ label: formModel.managerName ? formModel.managerName : '', value: formModel.manager ? formModel.manager[0] : '' }],
        params: {
          type: '1',
        },
      };
    },
  },
  // {
  //   field: 'epmPerson1Name',
  //   label: '分管领导',
  //   component: 'ApiTreeSelect',
  //   componentProps: ({ formModel }) => {
  //     return {
  //       api: getAsynKeyUser,
  //       parentCode: {
  //         key: 'id',
  //         parentKey: 'parentId',
  //       },
  //       fieldNames: {
  //         children: 'children',
  //         label: 'name',
  //         value: 'code',
  //       },
  //       multiple: true,
  //       // @ts-ignore
  //       onChange: (value, label, extra) => {
  //         formModel.epmPerson1 = [];
  //         formModel.epmPerson1Desc = [];
  //         extra.allCheckedNodes.forEach((item) => {
  //           formModel.epmPerson1.push(item.node.props.code);
  //           formModel.epmPerson1Desc.push(item.node.props.name);
  //         });
  //       },
  //       getPopupContainer: () => document.body,
  //     };
  //   },
  // },
  // {
  //   field: 'epmPerson1',
  //   label: '分管领导名称',
  //   component: 'Input',
  //   show: false,
  // },
  // {
  //   field: 'epmPerson1Desc',
  //   label: '分管领导名称',
  //   component: 'Input',
  //   show: false,
  // },
  // {
  //   field: 'epmPerson2Name',
  //   label: '主要领导',
  //   component: 'ApiTreeSelect',
  //   componentProps: ({ formModel }) => {
  //     return {
  //       api: getAsynKeyUser,
  //       parentCode: {
  //         key: 'id',
  //         parentKey: 'parentId',
  //       },
  //       fieldNames: {
  //         children: 'children',
  //         label: 'name',
  //         value: 'code',
  //       },
  //       multiple: true,
  //       // @ts-ignore
  //       onChange: (value, label, extra) => {
  //         formModel.epmPerson2 = [];
  //         formModel.epmPerson2Desc = [];
  //         extra.allCheckedNodes.forEach((item) => {
  //           formModel.epmPerson2.push(item.node.props.code);
  //           formModel.epmPerson2Desc.push(item.node.props.name);
  //         });
  //       },
  //       getPopupContainer: () => document.body,
  //     };
  //   },
  // },
  // {
  //   field: 'epmPerson2',
  //   label: '主要领导名称',
  //   component: 'Input',
  //   show: false,
  // },
  // {
  //   field: 'epmPerson2Desc',
  //   label: '主要领导名称',
  //   component: 'Input',
  //   show: false,
  // },
  {
    field: 'epmPerson2Name',
    label: '主要领导名称',
    component: 'Input',
    show: false,
  },
  {
    field: 'epmPerson2Code',
    label: '主要领导编码',
    component: 'Input',
    show: false,
  },
  {
    label: '主要领导',
    field: 'epmPerson2',
    required: false,
    component: 'ZSelectCustom',
    componentProps: ({ formModel }) => {
      console.log('formModel', formModel);
      return {
        sync: true,
        queryApi: selectPersonTreeList,
        titleKey: 'title',
        rowKey: 'psnNum',
        multiple: true, //多选
        modalTitle: '人员选择',
        showSearch: true,
        maxTagCount: 3,
        // options: [{ label: formModel.managerName ? formModel.managerName : '', value: formModel.manager ? formModel.manager[0] : '' }],
        params: {
          type: '1',
        },
      };
    },
  },
  {
    field: 'epmPerson3Name',
    label: '其它分管领导名称',
    component: 'Input',
    show: false,
  },
  {
    field: 'epmPerson3Code',
    label: '其它分管领导编码',
    component: 'Input',
    show: false,
  },
  {
    label: '其它分管领导',
    field: 'epmPerson3',
    required: false,
    component: 'ZSelectCustom',
    componentProps: ({ formModel }) => {
      console.log('formModel', formModel);
      return {
        sync: true,
        queryApi: selectPersonTreeList,
        titleKey: 'title',
        rowKey: 'psnNum',
        multiple: true, //多选
        modalTitle: '人员选择',
        showSearch: true,
        maxTagCount: 3,
        // options: [{ label: formModel.managerName ? formModel.managerName : '', value: formModel.manager ? formModel.manager[0] : '' }],
        params: {
          type: '1',
        },
      };
    },
  },
  {
    field: 'assess',
    label: '是否考核',
    component: 'ZSwitch',
    componentProps: {
      options: ['true', 'false'],
    },
  },
  {
    field: 'isIntensification',
    label: '是否集约化',
    component: 'ZSwitch',
    componentProps: {
      options: ['true', 'false'],
    },
  },
  {
    field: 'displayOrder',
    label: '排序',
    component: 'InputNumber',
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'orgName',
    label: '部门名称',
    component: 'Input',
  },
  // {
  //   field: 'orgCode',
  //   label: '部门编码',
  //   component: 'Input',
  // },
  {
    field: 'parentOrgCode',
    label: '上级部门',
    required: false,
    component: 'ZSelectDept',
    componentProps: () => {
      return {
        sync: false,
        titleKey: 'orgName',
        rowKey: 'orgCode',
        multiple: false, //多选
        maxTagCount: 3,
        isSuper: true,
        params: {
          status: 0,
        },
      };
    },
  },
  {
    field: 'orgType',
    label: '组织类型',
    component: 'Select',
    componentProps: {
      options: statusOptionsList,
      immediate: true,
      onChange: (e) => {},
      fieldNames: {
        label: 'text',
        value: 'value',
      },
      onOptionsChange: (options) => {
        // console.log('get options', options.length, options);
      },
    },
    colProps: { span: 8 },
  },
  {
    field: 'assess',
    label: '是否考核',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '是',
          value: 'true',
        },
        {
          label: '否',
          value: 'false',
        },
      ],
    },
  },
  {
    field: 'isIntensification',
    label: '是否集约化',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '是',
          value: 'true',
        },
        {
          label: '否',
          value: 'false',
        },
      ],
    },
  },
];

export function formatDeptTree(tree) {
  tree.forEach((t) => {
    t.title = t.deptName;
    t.key = t.deptCode;
    if (t.children) {
      formatDeptTree(t.children);
    }
  });
}

export function formatUserTree(tree) {
  tree.forEach((t) => {
    t.title = t.name;
    t.key = t.id;
    if (t.children) {
      formatDeptTree(t.children);
    }
  });
}
